package com.kis.service.channel.impl;

import com.kis.model.channel.*;
import com.kis.service.channel.ChannelService;
import com.kis.service.commonService.commonServiceImpl.CommonServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.Collections;
import java.util.List;

/**
 * Created by wanglong on 15-12-17.
 */
@Service("channelService")
public class ChannelServiceImpl extends CommonServiceImpl implements ChannelService {
    private Logger log = LoggerFactory.getLogger(ChannelServiceImpl.class);
    @Override
    public List<Country> getCountrys() {
        String hql = "select t from Country t where  t.id in (select distinct tt.countryId from ChannelCountry tt)";
        List<Country> countries = this.queryListByHql(hql,null);
        return countries;
    }

    @Override
    public List<SendType> getSendType() {
        String hql = "select t from SendType t";
        return this.queryListByHql(hql,null);
    }

    @Override
    public ChannelTypePrices getRegPrice(Long pid, Long countryId) {
        Double price = 0.0;
        String hql = "select t from ChannelTypePrices t where t.countryId=? and t.channelTypeId=?";
        List<ChannelTypePrices> channelTypePrices = this.queryListByHql(hql, countryId, pid);
        ChannelTypePrices channelTypePrices1 = null;
        if (channelTypePrices.size() > 0) {
            channelTypePrices1 = channelTypePrices.get(0);
        }
        return channelTypePrices1;
    }

    @Override
    public List<Channel> getBestChannel(Long countryId, Double weight, String sendType) {
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine engine = scriptEngineManager.getEngineByName("javascript");
        String hql = "select t from Channel t,ChannelCountry cc where t.id = cc.channelId and cc.countryId=? and t.sendType like ?";
        List<Channel> channelList = Collections.emptyList();
        channelList = this.queryListByHql(hql, countryId, "%" + sendType + "%");
        for (Channel channel : channelList) {
            if (channel.getPid() == 5) {
                channel.setPrice(getPrice(channel.getBillingMethod().replace("w", weight.toString()), engine));
            } else {
                ChannelTypePrices typePrices = this.getRegPrice(channel.getPid(), countryId);
                if(null==typePrices){
                    log.warn("渠道:"+channel.getChannelName()+"("+channel.getId()+") no prices at Country:"+countryId);
                    continue;
                }
                String billing = channel.getBillingMethod().replace("w", weight.toString()).replace("price", typePrices.getPrice().toString());
                if(billing.indexOf("regPrice")>-1){
                    billing = billing.replace("regPrice", typePrices.getRegisPrice().toString());
                }
                if(billing.indexOf("disposal")>-1){
                    billing = billing.replace("disposal", typePrices.getRegisPrice().toString());
                }
                channel.setPrice(getPrice(billing, engine));
            }
        }
        return channelList;
    }

    public Double getPrice(String billing, ScriptEngine scriptEngine) {
        Double result = 0.0;
        try {
            result = Double.parseDouble(scriptEngine.eval(billing).toString());
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return result;
    }
}
